//
//  HOPatrolListViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/29/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOPatrolListViewController.h"
#import "HOPotalEntity.h"
#import "HOConstants.h"
#import "UIImageView+WebCache.h"
#import "HOUtils.h"
@interface HOPatrolListViewController ()

@end

@implementation HOPatrolListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableViewWithPullUp];
	[self requestDataWithMaxId:1];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"巡查列表", @"巡查列表")];
	
}

- (void)requestDataWithMaxId:(int)maxId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetInspectList,
								  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId]}];
}

- (void)pullUpToRefresh
{
	HOPotalEntity *entity = [self.tableViewDataList lastObject];
	if(entity)
	{
		[self requestDataWithMaxId:entity.potalId];
	}
	else
	{
		[self requestDataWithMaxId:1];
	}
}


#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	NSInteger count = [self.tableViewDataList count];
	[self updateNoResultView:count];;
    return count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *HOTableViewCell = @"HOTableViewCell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTableViewCell];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier: HOTableViewCell] autorelease];
    }
	HOPotalEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.section];
	UIImageView *headerImgView = (UIImageView *)[cell viewWithTag:115];
	if(headerImgView == nil)
	{
		headerImgView = [[UIImageView alloc]
			   initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
										HO_HORIZONAL_PADDING,
										80,
										80)];
		headerImgView.tag = 115;
		[cell.contentView addSubview:headerImgView];
		[headerImgView release];
	}
	headerImgView = (UIImageView *)[cell viewWithTag:115];
	[headerImgView sd_setImageWithURL:[NSURL URLWithString:entity.imageUrl] placeholderImage:[UIImage imageNamed:@"morentu"]];
	
	UILabel *titleLbl = (UILabel *)[cell viewWithTag:116];
	if(titleLbl == nil)
	{
		titleLbl = [[UILabel alloc]
			   initWithFrame:CGRectMake(HO_HORIZONAL_PADDING + 90,
										HO_HORIZONAL_PADDING,
										tableView.frame.size.width - 210,
										22)];
		titleLbl.tag = 116;
		[cell.contentView addSubview:titleLbl];
		titleLbl.textColor = TEXT_COLOR_666666;
		titleLbl.font = [UIFont systemFontOfSize:15];
		[titleLbl release];
	}
	titleLbl = (UILabel *)[cell viewWithTag:116];
	titleLbl.text = entity.inspectLocation;
	
	UILabel *timeLbl = (UILabel *)[cell viewWithTag:117];
	if(timeLbl == nil)
	{
		timeLbl = [[UILabel alloc]
			   initWithFrame:CGRectMake(baseTableview.frame.size.width - 160,
										titleLbl.frame.origin.y,
										150,
										22)];
		timeLbl.tag = 117;
		timeLbl.textAlignment = NSTextAlignmentRight;
		[cell.contentView addSubview:timeLbl];
		timeLbl.textColor = TEXT_COLOR_999999;
		timeLbl.font = [UIFont systemFontOfSize:13];
		[timeLbl release];
	}
	timeLbl = (UILabel *)[cell viewWithTag:117];
	timeLbl.text = [HOUtils getDateFromTimeInterval:entity.time * 1000];
	
	UILabel *contentLbl = (UILabel *)[cell viewWithTag:118];
	if(contentLbl == nil)
	{
		contentLbl = [[UILabel alloc]
			   initWithFrame:CGRectMake(titleLbl.frame.origin.x,
										titleLbl.frame.origin.y + 25,
										tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING - 90,
										60)];
		contentLbl.tag = 118;
		contentLbl.numberOfLines = 3;
		[cell.contentView addSubview:contentLbl];
		contentLbl.textColor = TEXT_COLOR_999999;
		contentLbl.font = [UIFont systemFontOfSize:13];
		[contentLbl release];
	}
	contentLbl = (UILabel *)[cell viewWithTag:118];
	contentLbl.text = entity.content;
	
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
#pragma mark

- (HOEditTableViewCell *)tableView:(UITableView *)tableView cellForEditRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOEditTableViewCell *cell = (HOEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOEditTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	return cell;
}
#pragma mark -- tool
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetInspectList])
	{
		for(int i = 0; i < [response.dataNode count]; i ++)
		{
			NSDictionary *dic = [response.dataNode objectAtIndex:i];
			HOPotalEntity *entity = [[HOPotalEntity alloc] init];
			[entity parseDic:dic];
//			entity.location = @"大家理发店说两句分发店说两句分登陆手机莲登陆手机莲富大厦范德萨 发店说两句分登陆手机莲发店说两句分登陆手机莲水电费是否收到第三方";
			entity.content = entity.inspectLocation;
			[self.tableViewDataList addObject:entity];
			[entity release];
		}
		[self doneLoadingTableViewRefresh];
	}
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
